package com.tbit.uqbike.service.business.impl;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.tbit.uqbike.object.pojo.dto.ClockInManageDTO;
import com.tbit.uqbike.object.pojo.vo.AccountUserWorkVo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.ClockInManageService;
import com.tbit.uqbike.webmanager.dao.core.AccountUserDao;
import com.tbit.uqbike.webmanager.dao.log.ClockInManageDao;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author LHX
 * @ClassName ClockInManageServiceImpl
 * @Description
 * @date 2023/5/15 15:26
 * @Version 1.0
 */
@Service
public class ClockInManageServiceImpl implements ClockInManageService {

    @Autowired
    private ClockInManageDao clockInManageDao;

    @Autowired
    private AccountUserDao accountUserDao;

    @Override
    public PageResult<AccountUserWorkVo> queryPage(ClockInManageDTO dto) {
        //获取区域内所有运维人员信息
        Map<Integer, Map<String, String>> nameMap = accountUserDao.getNameByAccountId(dto);
        dto.setAccountUserIds(nameMap.keySet());
        PageInfo<AccountUserWorkVo> pageInfo = PageMethod.startPage(dto.getPage(), dto.getSize()).doSelectPageInfo(() -> clockInManageDao.queryPage(dto));
        if (!nameMap.isEmpty() || !pageInfo.getList().isEmpty()) {
            for (AccountUserWorkVo accountUserWorkVo : pageInfo.getList()) {
                Map<String, String> map = nameMap.get(accountUserWorkVo.getAccountUserId());
                if (map != null) {
                    accountUserWorkVo.setAccountUserName(map.get("NAME"));
                } else {
                    accountUserWorkVo.setAccountUserName("");
                }
            }
        }
        return new PageResult<>(dto.getPage(), dto.getSize(), pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getList());
    }

    @Override
    public Result exportClockInManage(ClockInManageDTO dto) {
        dto.setPage(1);
        dto.setSize(65535);
        PageResult<AccountUserWorkVo> result = queryPage(dto);
        List<AccountUserWorkVo> list = result.getList();
        ExcelUtil<AccountUserWorkVo> excel = new ExcelUtil<>(AccountUserWorkVo.class);
        JsonResult jsonResult = excel.exportExcel(list, "运维打卡记录");
        return Result.success(jsonResult.getData());
    }
}
